#!/usr/bin/python

import pickle
import os
import base64
import sys

# *********************************************************************
class Config(object):
	
	# -------------------------------------------------------------------
	def __init__(self, appname):
		self.appname = appname
		self.data = {}
		
		if os.name == 'posix':
			self.filename = os.environ['HOME']
		elif os.name == 'nt':
			self.filename = os.environ['HOMEPATH']
		
		self.filename += '/.%s' % self.appname		
	
	# -------------------------------------------------------------------
	def Load(self):
		try:
			f = file(self.filename, 'rb')			
			self.data = pickle.load(f)
			f.close()
		except EnvironmentError, e:
			print e
		
	# -------------------------------------------------------------------
	def Save(self):
		f = file(self.filename, 'wb')
		pickle.dump(self.data, f, pickle.HIGHEST_PROTOCOL)
		f.close()
		
	# -------------------------------------------------------------------
	def __getitem__(self, k):
		return self.data[k]
	
	# -------------------------------------------------------------------
	def __setitem__(self, k, v):
		self.data[k] = v
	
	# -------------------------------------------------------------------
	def __delitem__(self, k):
		del self.data[k]
		
	# -------------------------------------------------------------------
	def get(self, k, default = None):
		if self.data.has_key(k):
			return self.data[k]
		
		return default

# *********************************************************************
class StdoutLog(object):
	@classmethod
	def write(self, msg):
		sys.stdout.write(msg + '\n')
		
# *********************************************************************
class NullLog(object):
	@classmethod
	def write(self, msg):
		pass

# *********************************************************************
class FileLog(object):
	def __init__(self, filename):
		self.f = file(filename, 'w')
	
	def write(self, msg):
		self.f.write(msg + '\n')

